//
//  contactUsTVCell.m
//  UIScrollView的快速使用
//
//  Created by hongfei tam on 2019/3/5.
//  Copyright © 2019 YQ05. All rights reserved.
//

#import "contactUsTVCell.h"
#import "MKConstant.h"
#import "AppDelegate.h"

@interface contactUsTVCell()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{
    //UIView *BGview;
    UITextField *nameTF;
    UITextField *phoneTF;
    UILabel *sexLabel;
    UISwitch *mswitch;
    BOOL issexSW1On;
    
    NSArray *serverarray;
    NSArray *setLabelarr;
    
    UIPickerView *serverPickerView;
    NSString *serverStr;
    
    NSString *titleimg;
    NSString *title;
    
    UITextField *messTF;
    
    AppDelegate *delegate;
    //UIButton *submit;
}
@property (weak, nonatomic) IBOutlet UIView *BGview;

@end

@implementation contactUsTVCell

- (void)awakeFromNib {
    [super awakeFromNib];
     delegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
    //BGview = [[UIView alloc]initWithFrame:CGRectMake(60, 10, UISCREEN_WIDTH, 50)];
        _BGview.backgroundColor = [UIColor whiteColor];
        _BGview.layer.shadowColor = [UIColor grayColor].CGColor;
        _BGview.layer.shadowOffset = CGSizeMake(3, 3);

    
    sexLabel.text = @"女";
    issexSW1On = true;  //设置默认状态
    
   
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    //[super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+(instancetype)createCellWithTableView:(UITableView*)tableView{
    static NSString *ID = @"contactUsTVCell";
    contactUsTVCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        // xib加载cell
        cell = [[[NSBundle mainBundle]loadNibNamed:@"contactUsTVCell" owner:nil options:nil]lastObject];
    }
    return cell;
}
-(void)loadData:(NSString*)image label:(NSString*)label andprotitledic:(NSMutableDictionary*)protitledic{
    // self.titleimg.image = [UIImage imageNamed:image];
    //self.protitle.text = label;
    
    //    self.phoneLabel.text = [protitledic valueForKey:@"phoneLabel"];
    //    self.emailLabel.text = [protitledic valueForKey:@"emailLabel"];
    titleimg = image;
    title = label;
    //serverarray = @[@"- 請選擇 -",@"抗衰老療程",@"營養管理計劃",@"DNA檢測",@"醫療檢測",@"進階性美容療程",@"度身訂造修身/體重管理"];
    serverarray = [protitledic valueForKey:@"serverarray"];
    setLabelarr = [protitledic valueForKey:@"setLabelarr"];
    [self titleLabel];
    [self titleimg];
    [self nameLabel];
    [self setnameTF];
    [self phoneLabel];
    [self setphoneTF];
    
    [self titlesexLabel];
    [self setsexLabel];
    [self sexswitch1];
    
    
    [self serverLabel];
    [self setmpickerView];
    
    [self messLabel];
    [self setmessTF];
    
    [self submitBtn];
    // Initialization code
}
// 输入完成之后被调用
-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"text=%@",textField.text);
}
// 点击done之后被调用
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"tag=%ld",(long)textField.tag);
    [textField resignFirstResponder];//取消当前键盘输入响应
    return true;
}
-(void)titleLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, _BGview.bounds.size.width - 60, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text = title;// 内容
    label3.textColor = MENUBROWN; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:26.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [_BGview addSubview:label3];
}

-(void)titleimg{
    //  imageNamed:把一张图片读取进来
    UIImage *image1 = [UIImage imageNamed:titleimg];
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 30, 30)];
    imageview.image = image1;
    //UIImageView *imageview = [[UIImageView alloc]initWithImage:image1];
    [_BGview addSubview:imageview];
}

-(void)nameLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text = [setLabelarr objectAtIndex:0];// 内容
    label3.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [_BGview addSubview:label3];
}

// textfiled
-(void)setnameTF{
    nameTF = [[UITextField alloc]initWithFrame:CGRectMake(90, 60, (UISCREEN_WIDTH  - 60 - 90), 40)];
    nameTF.borderStyle = UITextBorderStyleLine;// 边框
    //text2.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //nameTF.secureTextEntry = YES; //密码
    nameTF.font = [UIFont systemFontOfSize:17.0];
    //nameTF.placeholder = @"請輸入密碼";// 提示
    
    //    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 300, 30)];
    //    textfield.borderStyle = UITextBorderStyleLine;// 边框
    //    textfield.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //    textfield.font = [UIFont systemFontOfSize:17.0];
    //    textfield.placeholder = @"please enter text";// 提示
    
    nameTF.keyboardType = UIKeyboardTypeDefault;
    //    UIKeyboardTypeDefault,                // Default type for the current input method.
    //    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters
    //    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    //    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    //    UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
    //    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    //    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    //    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    //    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
    //    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
    //    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
    //    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0),
    nameTF.returnKeyType = UIReturnKeyDone;
    //设置UITextField的边框样式
    nameTF.borderStyle = UITextBorderStyleRoundedRect;//圆角
    //    UITextBorderStyleNone, // 无边框
    //    UITextBorderStyleLine, // 有黑色边框
    //    UITextBorderStyleBezel, // 有灰色边框和阴影
    //    UITextBorderStyleRoundedRect // 圆角
    
    nameTF.clearButtonMode = UITextFieldViewModeWhileEditing; //输入框右侧删除按钮  在UITextField文本框中开始编辑的时候显示清除设置，点击清除按钮删除内容
    nameTF.delegate = self;// 谁来响应
    [_BGview addSubview:nameTF];
}

-(void)phoneLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 110, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text = [setLabelarr objectAtIndex:1];// 内容
    label3.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [_BGview addSubview:label3];
}

// textfiled
-(void)setphoneTF{
    phoneTF = [[UITextField alloc]initWithFrame:CGRectMake(90, 110,  (UISCREEN_WIDTH  - 60 - 90), 40)]; //BGview.bounds.size.width -
    phoneTF.borderStyle = UITextBorderStyleLine;// 边框
    //text2.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //nameTF.secureTextEntry = YES; //密码
    phoneTF.font = [UIFont systemFontOfSize:17.0];
    //nameTF.placeholder = @"請輸入密碼";// 提示
    
    //    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 300, 30)];
    //    textfield.borderStyle = UITextBorderStyleLine;// 边框
    //    textfield.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //    textfield.font = [UIFont systemFontOfSize:17.0];
    //    textfield.placeholder = @"please enter text";// 提示
    
    phoneTF.keyboardType = UIKeyboardTypePhonePad;
    
    phoneTF.returnKeyType = UIReturnKeyDone;
    //设置UITextField的边框样式
    phoneTF.borderStyle = UITextBorderStyleRoundedRect;//圆角
    
    phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing; //输入框右侧删除按钮  在UITextField文本框中开始编辑的时候显示清除设置，点击清除按钮删除内容
    phoneTF.delegate = self;// 谁来响应
    
    [delegate addDoneToKeyboard:phoneTF];
    [_BGview addSubview:phoneTF];
}

#pragma mark -- Switch
// 开关：开 关
-(void)sexswitch1{
    mswitch = [[UISwitch alloc]initWithFrame:CGRectMake(90, 160, 50, 25)];
    [mswitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    mswitch.on =true; //switch 默认状态
    [_BGview addSubview:mswitch];
    
}
-(void)switchAction:(UISwitch*)sender{
    //NSLog(@"%d",[sender isOn]);
    //isproSW1On = [_proSW1 isOn];
    //NSLog(@"isproSW1On 现在的状态 %id",issexSW1On);
    issexSW1On = !issexSW1On;
    if (issexSW1On) {
        [self btmethodstatus:true];
    }else {
        [self btmethodstatus:false];
    }
}
-(void)btmethodstatus:(BOOL)status{
    if (status) {
        //NSLog(@"點擊了true ");
        sexLabel.text = @"女";
    }else{
        //NSLog(@"點擊了false ");
        sexLabel.text = @"男";
    }
    //NSLog(@" SWstatus %id",status); //按键
    NSLog(@"issexSW1On %id",issexSW1On); //状态
}

-(void)titlesexLabel{
    //  3 字体
    UILabel *Label1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 150, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    Label1.text = [setLabelarr objectAtIndex:2];// 内容
    Label1.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    Label1.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    Label1.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [_BGview addSubview:Label1];
}

-(void)setsexLabel{
    //  3 字体
    sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 150, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    sexLabel.text = @"女";// 内容
    sexLabel.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    sexLabel.font = [UIFont systemFontOfSize:16.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    sexLabel.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [_BGview addSubview:sexLabel];
}

#pragma mark -- pickView
-(void)serverLabel{
    //  3 字体
    UILabel *Label1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 250, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    Label1.text = [setLabelarr objectAtIndex:3];// 内容
    Label1.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    Label1.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    Label1.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [_BGview addSubview:Label1];
}

- (void)setmpickerView {
    serverPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 190, 230, 160)];
    serverPickerView.delegate = self;
    serverPickerView.dataSource = self;
    [_BGview addSubview:serverPickerView];
}
// 组 几个部分
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
// 每个组 由多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return serverarray.count;
    
}
// 每个cell的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *str = nil;
    
    str = [serverarray objectAtIndex:row];
    return str;
}
// 选择某一行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    serverStr = [serverarray objectAtIndex:row];
    NSLog(@"服務項目： %@",serverStr);
}
//pickerView富文本属性: 可以描述文字大小和颜色
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = [UIColor clearColor];
        }
    }
    
    
    //设置文字的属性
    UILabel *pLabel = [[UILabel alloc] init];
    pLabel.textAlignment = NSTextAlignmentCenter;
    pLabel.text = serverarray[row];
    pLabel.font = [UIFont systemFontOfSize:16.0];
    //[pLabel sizeToFit];
    pLabel.textColor =[UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1.0];
    //[aview addSubview:pLabel];
    return pLabel;
}

#pragma mark -- 信息
-(void)messLabel{
    //  3 字体
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 350, 70, 50)];
    //label3.backgroundColor = MENUBROWN;// 背景色
    label3.text = [setLabelarr objectAtIndex:4];// 内容
    label3.textColor = [UIColor grayColor]; //内容颜色
    //  使用字体名称
    //label3.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];
    label3.font = [UIFont systemFontOfSize:14.0];
    //  对其方式: text内容 相对 uilabel command＋鼠标左键
    label3.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
    
    [_BGview addSubview:label3];
}

// textfiled
-(void)setmessTF{
    messTF = [[UITextField alloc]initWithFrame:CGRectMake(90, 350, (UISCREEN_WIDTH  - 60 - 90), 40)]; //BGview.bounds.size.width -
    messTF.borderStyle = UITextBorderStyleLine;// 边框
    //text2.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //nameTF.secureTextEntry = YES; //密码
    messTF.font = [UIFont systemFontOfSize:17.0];
    //nameTF.placeholder = @"請輸入密碼";// 提示
    
    //    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 300, 30)];
    //    textfield.borderStyle = UITextBorderStyleLine;// 边框
    //    textfield.textColor = [UIColor redColor];// text 字体颜色 内容字体大小
    //    textfield.font = [UIFont systemFontOfSize:17.0];
    //    textfield.placeholder = @"please enter text";// 提示
    
    messTF.keyboardType = UIKeyboardTypeDefault;
    
    messTF.returnKeyType = UIReturnKeyDone;
    //设置UITextField的边框样式
    messTF.borderStyle = UITextBorderStyleRoundedRect;//圆角
    
    messTF.clearButtonMode = UITextFieldViewModeWhileEditing; //输入框右侧删除按钮  在UITextField文本框中开始编辑的时候显示清除设置，点击清除按钮删除内容
    messTF.delegate = self;// 谁来响应
    
    [_BGview addSubview:messTF];
}

#pragma mark -- submitBtn
-(void)submitBtn{
    //   1 UI实例化 2 UI属性 3view 添加
    //initWithFrame:UI实例化 Frame
    //  UI+XX
//    NSLog(@"BGview.bounds.size.width %f",_BGview.bounds.size.width);
//    NSLog(@"UISCREEN_WIDTH %f",UISCREEN_WIDTH);
    //UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((BGview.bounds.size.width - 120)/2 , 370, 140, 40)]; //(BGview.bounds.size.width - 140)/2
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((UISCREEN_WIDTH - 140)/2 , 410, 140, 40)];
    //  作用：为Button 添加一个内容 1:title 2状态
    [button setTitle:@"提交" forState:UIControlStateNormal];
    //  作用：为Button 内容设置颜色 1:title颜色 2状态
    [button setTitleColor:MENUBROWN forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize: 16.0];  //字體大小
    
    [button setBackgroundImage:[UIImage imageNamed:@"botton_140x40.jpg"] forState:UIControlStateNormal];
    
    //但是问题又出来，此时文字会紧贴到做边框，我们可以设置
    //button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    //使文字距离做边框保持10个像素的距离。
    //button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
    
    
    
    //  作用：是为button添加响应方法 1 self 2@selector(方法名) 3UIControlEventTouchUpInside:点击之后
    //  :
    [button addTarget:self action:@selector(submitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_BGview addSubview:button];
}
//  谁调用 bt 谁传递过来的
-(void)submitBtnAction{
    NSLog(@"contactUsTVcellsend");
  
    NSMutableDictionary *dic = [self submitAction];
    NSLog(@"提交字典  %@",dic);
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"contactUsTVcellsend" object:nil userInfo:dic];
    
    
}


-(NSMutableDictionary*)submitAction{
    [nameTF resignFirstResponder];
    [phoneTF resignFirstResponder];
    [messTF resignFirstResponder];
    NSString *prostr1 = [nameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *prostr2 = [phoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *prostr4 = [messTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *servertiem = [serverStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSString *sexstr = @"";
    if (issexSW1On) { //性别值
        //sexstr = @"女";
        sexstr = @"F";
    }else{
        //sexstr = @"男";
        sexstr = @"M";
    }
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:prostr1 forKey:@"name"];
    [dic setValue:prostr2 forKey:@"phone"];
    [dic setValue:prostr4 forKey:@"mess"];
    [dic setValue:sexstr forKey:@"sex"];
    [dic setValue:servertiem forKey:@"servertiem"];
    [dic setValue:@"contact" forKey:@"action"]; //action=contact ：咨询
    return dic;
}


@end
